/*! ----------------------------------------------------------------------------
 * @file	deca_spi.h
 * @brief	SPI access functions
 *
 * @attention
 *
 * Copyright 2015 (c) DecaWave Ltd, Dublin, Ireland.
 *
 * All rights reserved.
 *
 * @author DecaWave
 */

#ifndef _DECA_SPI_H_
#define _DECA_SPI_H_

#ifdef __cplusplus
extern "C" {
#endif

#include "deca_types.h"



#define SPI_TypeDef char
#define SPI01 "/dev/spidev0.0"
#define SPI02 "/dev/spidev0.1"
#define assert_param(expr) ((void)0)
#define FunctionalState int
#define DISABLE 0
#define ENABLE 1



#define DECA_MAX_SPI_HEADER_LENGTH      (3)                     // max number of bytes in header (for formating & sizing)
#define EVB1000_LCD_SUPPORT				(1)
/*! ------------------------------------------------------------------------------------------------------------------
 * Function: openspi()
 *
 * Low level abstract function to open and initialise access to the SPI device.
 * returns 0 for success, or -1 for error
 */
int openspi(void) ;

/*! ------------------------------------------------------------------------------------------------------------------
 * Function: closespi()
 *
 * Low level abstract function to close the the SPI device.
 * returns 0 for success, or -1 for error
 */
int closespi(void) ;



void SPI_Init(SPI_TypeDef* SPIx);
void SPI_read(SPI_TypeDef* SPIx, char *buf, size_t count);
void SPI_write(SPI_TypeDef* SPIx, char *buf, size_t count);

#if 1
int writeto_spi_serial( uint16 headerLength,
			   	    const uint8 *headerBuffer,
					uint32 bodylength,
					const uint8 *bodyBuffer
				  );

int readfrom_spi_serial( uint16	headerLength,
			    	 const uint8 *headerBuffer,
					 uint32 readlength,
					 uint8 *readBuffer );

#endif

#if 0
int readfrom_spi_serial
(
    unsigned char       headerLength,
    unsigned char * headerBuffer,
    unsigned long       readlength,
    unsigned char       *readBuffer
);

int writeto_spi_serial
(
    unsigned char       headerLength,
    unsigned char	*headerBuffer,
    unsigned int       bodylength,
    unsigned char	*bodyBuffer
);
#endif


#if (EVB1000_LCD_SUPPORT == 1)
/*! ------------------------------------------------------------------------------------------------------------------
 * Function: writetoLCD()
 *
 * Low level abstract function to write data to the LCD display via SPI2 peripheral
 * Takes byte buffer and rs_enable signals
 * or returns -1 if there was an error
 */
void writetoLCD
(
    uint32       bodylength,
    uint8        rs_enable,
    const uint8 *bodyBuffer
);
#else
#define writetoLCD(x)
#endif

#ifdef __cplusplus
}
#endif

#endif /* _DECA_SPI_H_ */



